package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.scity.beens.HouseInfoBeen;
import com.example.scity.beens.NewsInfoBeen;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HouseDetailActivity extends AppCompatActivity {
    ImageView houseImageView;
    TextView houseView;
    TextView addressView;
    TextView areaView;
    TextView priceView;
    TextView telView;
    TextView desView;
    int id;
    HouseInfoBeen houseInfoBeen;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                houseView = findViewById(R.id.textView_house);
                addressView = findViewById(R.id.textView_address);
                areaView = findViewById(R.id.textView_area);
                priceView = findViewById(R.id.textView_price);
                telView = findViewById(R.id.textView_phone);
                desView = findViewById(R.id.textView_des);
                houseImageView = findViewById(R.id.imageView_house);
                houseView.setText(houseInfoBeen.data.sourceName);
                addressView.setText(houseInfoBeen.data.address);
                priceView.setText(houseInfoBeen.data.price);
                telView.setText(houseInfoBeen.data.tel);
                desView.setText(houseInfoBeen.data.description);
                //addressView.setText(Html.fromHtml(houseInfoBeen.data.content));
                Glide.with(HouseDetailActivity.this).load(Configs.Host+houseInfoBeen.data.pic).into(houseImageView);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_detail);

        Intent intent = getIntent();
        id = intent.getIntExtra("ID",0);

    }
    private void loadinfo() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+ APIInterface.HouseInfo+id).build();//http://124.93.196.45:10001/prod-api/press/press/29
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                houseInfoBeen = new Gson().fromJson(response.body().string(), HouseInfoBeen.class);
                handler.sendEmptyMessage(1);//加载成功
            }
        });
    }
}